<?php
namespace Setting\Controller;

use Base\Controller\AdminBaseController;

class ContentController extends AdminBaseController{
	
	protected $contentModel;
	
	public function _initialize() {
		parent::_initialize();
		$this->contentModel = D("Setting/Content");
	}

	/******************************** JSON *********************************/
	/** 添加信息 */
	public function save() {
		$modelJson = file_get_contents('php://input');
		$model = json_decode($modelJson, true);
		
		// setting
		$model['status'] = 1;

		// 执行添加
		$result = $this->contentModel->add($model);
		if (!$result) {
			$data['code'] = 499;
			$data['msg'] = '添加失败！';
			$this->ajaxReturn($data);
		}

		$newContent = $this->contentModel->where(array("id" => $result))->find();
			
		$data['code'] = 0;
		$data['msg'] = '添加成功！';
		$data['obj'] = $newContent;
		$this->ajaxReturn($data);
	}
	
	/** 查看所有  */
    public function getAll() {
		$result = $this->contentModel->select();
// 		dump($result);
		
	    // return
	    $this->ajaxReturn($result);
	}

	/** 查看所有列表 */
	public function dataList() {
	    $_GET['notEqualMap'] = array('status' => -1);
	    $dataList = $this->contentModel->dataList($_GET);
	    $this->ajaxReturn($dataList);
	}

	/** 根据cid查看所有列表 */
	public function dataListByContentCatId() {
	    $contentCatId = $_GET['cid'];
	     
	    $contentCatList = M("ContentCat")->select();
	    $idsList = $this->getIds($contentCatList, $contentCatId, []);
// 	    dump($idsList);
	    
		$_GET['inKey'] = 'cid';
		$_GET['inValue'] = $idsList;
		$_GET['notEqualMap'] = array('status' => -1);
		$dataList = $this->contentModel->dataList($_GET);
		
	    $this->ajaxReturn($dataList);
	}
	 
	/** 查看某信息 */
	public function find() {
		$id = $_GET['id'];
		
		$model = $this->contentModel->where(array("id" => $id))->find();
		if (!$model) {
			$data['code'] = 499;
			$data['msg'] = '未找到目标！';
			$this->ajaxReturn($data);
		}
		
	    $data['code'] = 0;
	    $data['msg'] = 'OK';
	    $data['obj'] = $model;
	    $this->ajaxReturn($data);
	}

	/** 修改某信息 */
	public function update() {
		$id = $_GET['id'];
		$modelJson = file_get_contents('php://input');
		$model = json_decode($modelJson, true);
		
		// 执行更新
		$result = $this->contentModel->where(array("id" => $id))->save($model);
		if (false === $result) {
			$data['code'] = 499;
			$data['msg'] = '编辑失败！';
			$this->ajaxReturn($data);
		}

		$updatedPost = $this->contentModel->where(array("id" => $id))->find();
			
		$data['code'] = 0;
		$data['msg'] = '编辑成功！';
		$data['obj'] = $updatedPost;
		$this->ajaxReturn($data);
	}

	/** 审核 */
	public function enable() {
		$id = $_GET['id'];
		$status = $_GET['status'];
		
		$user = $this->contentModel->where(array("id" => $id))->find();
		if (!$user) {
			$data['code'] = 499;
			$data['msg'] = '未找到目标！';
			$this->ajaxReturn($data);
		}
		
		// 更新status & update
		$user['status'] = $status;
		$user['update_time'] = date("Y-m-d H:i:s");
		
		// 执行更新
		$result = $this->contentModel->where(array("id" => $id))->save($user);
		if (false === $result) {
			$data['code'] = 499;
			$data['msg'] = '操作失败！';
			$this->ajaxReturn($data);
		}

		$data['code'] = 0;
		$data['msg'] = '操作完成！';
		$data['obj'] = $user;
		$this->ajaxReturn($data);
	}
	
	/** 删除某信息 */
	public function delete() {
		$id = $_GET['id'];
		
		$model = $this->contentModel->where(array("id" => $id))->find();
		if (!$model) {
			$data['code'] = 499;
			$data['msg'] = '未找到目标！';
			$this->ajaxReturn($data);
		}
		
		// 更新status & update
		$model['status'] = -1;

		$result = $this->contentModel->where(array("id" => $id))->save($model);
		if (false === $result) {
			$data['code'] = 499;
			$data['msg'] = '删除失败！';
			$this->ajaxReturn($data);
		}
			
		$data['code'] = 0;
		$data['msg'] = '删除成功！';
		$this->ajaxReturn($data);
	}

	/******************************** HTML *********************************/
	/** index */
    public function index() {
		$this->display(':content');
		return;
	}

	/**----------------------------------- do ----------------------------*/
	public function getIds($data, $pid = 0, $idsList = []) {
	    $idsList[] = (int)$pid;
	    foreach ($data as $k => $v) {
	        if ($v['pid'] == $pid) {
	            $idsList = $this->getIds($data, $v['id'], $idsList);
	        }
	    }
	    return $idsList;
	}
}